package com.yulongtian.oneProblemEveryDay.month202301;

/**
 * https://leetcode.cn/problems/smallest-string-with-a-given-numeric-value/
 * 可
 *
 * @author yulongTian
 * @create 2023-01-26 12:34
 */
public class Test17 {
    public static void main(String[] args) {
        System.out.println(getSmallestString(5, 73));
    }

    //n = 5; k = 73
    public static String getSmallestString(int n, int k) {
        int num = 0;
        StringBuilder ans = new StringBuilder();
        for (int i = 1; i <= n; i++) {
            for (int j = 25; j >= 0; j--) {
                num += j + 1;
                char temp = (char) ('a' + j);
                if (num < k) {
                    if ((k - num) < (n - ans.length() - 1)) {
                        num -= (j + 1);
                        continue;
                    }
                    ans.append((char) ('a' + j));
                    break;
                } else if (num == k) {
                    if (ans.length() == n - 1) {
                        ans.append((char) ('a' + j));
                        return ans.reverse().toString();
                    } else {
                        num -= (j + 1);
                    }
                } else {
                    num -= (j + 1);
                }
            }
        }
        return ans.reverse().toString();
    }

}
